package com.kyle.pagingdemo.ui.fragment

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.core.view.doOnPreDraw
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.navArgs
import androidx.paging.ExperimentalPagingApi
import androidx.transition.TransitionInflater
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.transition.MaterialContainerTransform
import com.google.android.material.transition.MaterialFade
import com.google.android.material.transition.MaterialSharedAxis
import com.google.android.material.transition.platform.MaterialElevationScale
import com.kyle.pagingdemo.R
import com.kyle.pagingdemo.viewmodel.PagingViewModel
import com.kyle.pagingdemo.ViewPager3DTransform
import com.kyle.pagingdemo.ui.adapter.ViewPagerAdapter
import com.kyle.pagingdemo.databinding.FragmentDetailBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import javax.inject.Inject


@AndroidEntryPoint
class DetailFragment : Fragment() {

    private val pagingViewModel: PagingViewModel by activityViewModels()

    @Inject
    lateinit var adapter: ViewPagerAdapter
    private lateinit var binding: FragmentDetailBinding
    private val args: DetailFragmentArgs by navArgs()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        sharedElementEnterTransition = MaterialContainerTransform().apply {
            drawingViewId = R.id.fragment
            scrimColor = Color.TRANSPARENT
        }
//        enterTransition = MaterialSharedAxis(
//            MaterialSharedAxis.Z, /* forward = */ true)
//        returnTransition = MaterialSharedAxis(
//            MaterialSharedAxis.Z, /* forward = */ false)
    }

    @RequiresApi(Build.VERSION_CODES.M)
    @ExperimentalPagingApi
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        Log.d("DetailFragment", "onCreateView: ")
        lifecycleScope.launch {
            pagingViewModel.postFlow.collectLatest { pagingData ->
                adapter.submitData(pagingData)
            }
        }
        binding = FragmentDetailBinding.inflate(inflater, container, false)
        val viewPager = binding.viewPager
        Log.d("TAG", "DetailFragment: $pagingViewModel")
        val position = args.index
        Log.d("TAG", "position: $position")
        viewPager.transitionName = getString(R.string.container_card_item, position)
        viewPager.adapter = adapter
        viewPager.setPageTransformer(ViewPager3DTransform())
        viewPager.setCurrentItem(position, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        Log.d("DetailFragment", "onViewCreated: ${binding.viewPager.currentItem}")
        postponeEnterTransition()
         view.doOnPreDraw { startPostponedEnterTransition() }
    }
}